/**
 * Created by polaris on 2016/9/9.
 */


/*
 * 任务页面导航栏下方的
 * menu菜单
 * 滑动条动画效果处理
 */

(function ($) {

    //获取页面间的url的参数
    (function ($) {
        $.getUrlParam = function (name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return unescape(r[2]);
            return null;
        }
    })(jQuery);

    var projectCommentBack = $.getUrlParam('projectCommentBack');
    var taskBackType = $.getUrlParam('taskListBack');

    var htmlType = $.getUrlParam("htmlType");
    if (htmlType == null) {
        htmlType = 0;
    }
    var turnDay = $.getUrlParam('turnDay');
    if (turnDay != undefined) {
        localStorage.turnDay = turnDay;
    }
    //任务页面
    var taskHtml = '<div class="content-div">' +
        '<div id="charge">' +
        '<div class="charge-div" id="charge-delay">' +
        '<p class="charge-p">延期 <code class="charge-code" id="delay-count">（0）</code></p>' +
        '<div class="divLine" ></div>' +
        '<ul class="charge-ul" id="delay"></ul>' +
        '</div>' +
        '<div class="charge-div" id="charge-today">' +
        '<p class="charge-p">今天 <code class="charge-code" id="today-count">（0）</code></p>' +
        '<div class="divLine" ></div>' +
        '<ul class="charge-ul" id="today"></ul>' +
        '</div>' +
        '<div class="charge-div" id="charge-later">' +
        '<p class="charge-p">以后 <code class="charge-code" id="later-count">（0）</code></p>' +
        '<div class="divLine" ></div>' +
        '<ul class="charge-ul" id="later"></ul>' +
        '</div>' +
        '</div>' +
        '<div id="initiate">' +
        '<ul class="initiate-ul" id="public"></ul>' +
        '</div>' +
        '<div id="follow">' +
        '<ul class="charge-ul" id="follower" style="float: left"></ul>' +
        '</div>' +
        '<div class="task-div" id="task-complete">' +
        '<div class="task">' +
        '<p class="task-complete-p">查看已完成任务</p>' +
        '<img class="complete-row" src="../task/images/task/arrowRightImg.png" />' +
        '<div class="clear"></div>' +
        '</div>' +
        '<div class="divLine" ></div>' +
        '<ul class="finish-ul"></ul>' +
        '</div>' +
        '<div class="clear"></div>' +
        '</div>' +
        '<div class="noDataContent-div">' +
        '<img id="task-img" src="../task/images/task/task.png" />' +
        '<p class="task-tip">你还未创建任务</p>' +
        '</div>';

    //项目页面
    var projectHtml = '<div class="projectContent-div">' +
        '<ul class="project-ul">' +
        '</ul>' +
        '<div class="clear"></div>' +
        '</div>';

    //动态页面
    var dynamicHtml = '<div class="noTask-div">' +
        '<img id="noTask-img" src="../task/images/common/noComment.png" />' +
        '<p class="noTask-tip">暂无动态消息</p>' +
        '</div>' +
        '<div class="dynamicContent-div"></div>';

    var project_id = $.getUrlParam('project_id');    //项目ID
    var tabbarCount = $.getUrlParam('tabbarCount');  //项目页面

    var tasksMembers = [];
    var projectMembers = [];
    var projectTaskMembers = [];
    var projectCommentMembers = [];

    var stop = true;//触发开关，防止多次调用事件

    var isFynamic = false;
    var lastTime = "";
    var lastTimeStamp = 0;
    var userArray = [];

    if (tabbarCount != 1) {
        //任务页面
        if (taskBackType == undefined) {
            taskBackType = 0;
        }
        //0，1，2 表示我负责的，我发起的，我关注的
        var listType = taskBackType;
    }
    else {
        //项目页面
        if (projectCommentBack == 1) {
            //项目讨论列表
            var listType = 4;
        }
        else {
            //项目任务列表
            var listType = 3;
        }
        var lastComment = 0;
        var lastFileId = 0;
    }

    //下载附件参数
    var file_data_down = '';
    var file_name_down = '';

    //点击下载文件
    var downloadFile;
    downloadFile = document.createElement('iframe');
    downloadFile.style.display = 'none';
    document.documentElement.appendChild(downloadFile);
    $(document).on("click", ".call_back_down_p", function () {
        $(".call_back_down_p").hide();
    });

    $(document).on("click", "#cancel_down", function () {
        $(".call_back_down_p").hide();
    });

    $(document).on("click", "#sure_down", function () {
        $(".call_back_down_p").hide();
        downloadFile.src = "/downloadFile_downThisFile?url=http://" + file_data_down + "&fileName=" + file_name_down + "";
    });

    //footer任务\项目\动态切换
    function taskShow() {
        isFynamic = false;
        $(".loading").hide();
        $(".content-div").remove();
        $(".projectContent-div").remove();
        $(".dynamicContent-div").remove();
        $(".noDataContent-div").remove();
        $(".noTask-div").remove();
        $(".menu").show();
        $(".loading-div").append(taskHtml);
        $("#task").attr("src", "images/common/taskSelected.png");
        $("#dynamic").attr("src", "images/common/dynamicDisSelect.png");
        $("#project").attr("src", "images/common/projectDisSelect.png");
        $("#footerTaskSpan").css("color", "#75C9F5");
        $("#footerProjectSpan").css("color", "#929292");
        $("#footerDynamicSpan").css("color", "#929292");
        $(".title").text("任务");
        $(".navigation-rightBtn").text("新任务");
        $(".navigationRight").attr("type", "0");
        menuSlide();
        toggleDisplay();
        //任务首页
        var $chargeDiv = $("#charge");
        var $initiateDiv = $("#initiate");
        var $followDiv = $("#follow");

        if (listType == 0) {
            $chargeDiv.css("display", "block");
            $initiateDiv.css("display", "none");
            $followDiv.css("display", "none");
            $(".task-div").show();
            $(".show-hide-task").hide();
        }
        else if (listType == 1) {
            $chargeDiv.css("display", "none");
            $initiateDiv.css("display", "block");
            $followDiv.css("display", "none");
            $(".task-div").show();
            $(".show-hide-task").hide();
        }
        else {
            $chargeDiv.css("display", "none");
            $initiateDiv.css("display", "none");
            $followDiv.css("display", "block");
            $(".task-div").hide();
            $(".show-hide-task").hide();
        }
        projectTaskListWithMe(user_id);
    }

    function projectShow() {
        isFynamic = false;
        $(".taskLoading").hide();
        $(".projectContent-div").remove();
        $(".dynamicContent-div").remove();
        $(".noTask-div").remove();
        $(".content-div").remove();
        $(".noDataContent-div").remove();
        $(".menu").hide();
        $(".loading-div").append(projectHtml);
        $("#task").attr("src", "images/common/taskDisSelect.png");
        $("#dynamic").attr("src", "images/common/dynamicDisSelect.png");
        $("#project").attr("src", "images/common/projectSelected.png");
        $("#footerTaskSpan").css("color", "#929292");
        $("#footerProjectSpan").css("color", "#75C9F5");
        $("#footerDynamicSpan").css("color", "#929292");
        $(".title").text("项目");
        $(".navigation-rightBtn").text("新项目");
        $(".navigationRight").attr("type", "1");
        projectLists(user_id);
    }

    function dynamicShow() {
        lastTime = "";
        isFynamic = true;
        $(".taskLoading").remove();
        $(".projectContent-div").remove();
        $(".content-div").remove();
        $(".dynamicContent-div").remove();
        $(".noDataContent-div").remove();
        $(".menu").hide();
        $(".loading-div").append(dynamicHtml);
        $("#task").attr("src", "images/common/taskDisSelect.png");
        $("#dynamic").attr("src", "images/common/dynamicSelected.png");
        $("#project").attr("src", "images/common/projectDisSelect.png");
        $("#footerTaskSpan").css("color", "#929292");
        $("#footerProjectSpan").css("color", "#929292");
        $("#footerDynamicSpan").css("color", "#75C9F5");
        $(".title").text("动态");
        $(".navigation-rightBtn").text("");
        $(".navigationRight").attr("type", "2");
        dynamicLists(user_id, 0);
    }

    function tabbarAction() {
        $(document).on("click", ".navigationRight", function () {
            var type = parseInt($(this).attr("type"));
            switch (type) {
                case 0:
                    window.location.href = "html/task/taskNewTask.html?backType=" + listType + "";
                    break;
                case 1:
                    window.location.href = "html/project/newProject.html?type=createNew";
                    break;
                case 2:
                    break;
            }

        });
        $(document).on("click", "#footerTask", function () {
            listType = 0;
            taskShow();
        });
        $(document).on("click", "#footerProject", function () {
            projectShow();
        });
        $(document).on("click", "#footerDynamic", function () {
            dynamicShow();
        });
    }

    $(function () {

        if (tabbarCount != 1) {
            tabbarAction();
            if (htmlType == 1) {
                projectShow();
            }
            else if (htmlType == 2) {
                dynamicShow();
            }
            else {
                taskShow();
            }
        }
        else {
            menuSlide();
            toggleDisplay();
            projectDetail(project_id);
        }
        //设置按钮方法
        $("#setting").click(
            function () {
                window.location.href = "newProject.html?project_id=" + project_id + "";
            }
        );

        //设置项目详情返回按钮方法
        $("#projectDetailBack").click(
            function () {
                window.location.href = "../../index.html?htmlType=1";
                //window.history.back();
            }
        );

        //设置上传文件和创建任务按钮方法
        $(".noTaskButton-div").click(
            function () {
                if ($(".noTaskButton-div").text() == "创建任务") {
                    //创建任务
                    window.location.href = "projectCreateNewTask.html?project_id=" + project_id + "";
                }
                else {
                    //上传文件
                    $("#upload_file").click();
                }
            }
        )

        //添加新任务按钮方法
        $("#add-a").click(
            function () {
                window.location.href = "projectCreateNewTask.html?project_id=" + project_id + "";
            }
        )

        $(document).on("click", "#add-file", function () {
            $("#upload_file").click();
        });

        $(document).on('change', '#upload_file', function () {

            var formdata = new FormData($("#form_file")[0]);
            var file = this.files[0];
            if (file != undefined) {
                var file_name = file.name;
                var file_size_k = file.size;
                var file_size;
                if(file_size_k<1000){
                    file_size = Math.round(file_size_k) + "B";
                }else if(file_size_k<1000000){
                    file_size = Math.round(file_size_k/1024) + "KB";
                }else{
                    file_size = Math.round(file_size_k/1024/1024) + "MB";
                }

                var file_name = file.name;

                if (checkFileType(file_name) == true) {
                    formdata.append("type", "1");
                    formdata.append("file_size", file_size);

                    $.ajax({
                        type: 'POST',
                        url: apiurl + '/global/singleUpload',
                        data: formdata,
                        cache: false,
                        processData: false,
                        contentType: false,
                        mimeType: "multipart/form-data",
                        beforeSend: function () {
                            $(".change-p").text("正在上传附件");
                            $(".change-alert").show();
                        },
                        complete: function (d) {
                            $(".change-alert").hide();
                        },
                        success: function (d) {
                            var data = d;
                            if (d.code == undefined) {
                                data = JSON.parse(d);
                            }
                            if (data.code == 0) {
                                var dataFile = JSON.parse(d).data;
                                var data_url = dataFile.file_url;
                                uploadFile(user_id, project_id, file_name, data_url);
                            }
                            else {
                                $(".project-alert").text("Error").show().fadeOut(2000);
                            }
                        },
                        error: function (d) {
                            $(".project-alert").text("网络不给力").show().fadeOut(2000);
                        }
                    });
                } else {
                    $(".call_back_success").text("不支持该文件类型!").fadeIn();
                    setTimeout(function () {
                        $(".call_back_success").fadeOut(1000);
                    }, 1000);
                }
            }
            var fileInput = $('#upload_file');
            fileInput.replaceWith(fileInput.val('').clone(true));
        });
    });

    function menuSlide() {
        var $allEle;
        var $currentEle;
        //任务首页
        var $chargeDiv = $("#charge");
        var $initiateDiv = $("#initiate");
        var $followDiv = $("#follow");

        //项目详情
        var $projectTask = $("#projectTask");
        var $projectDiscussion = $("#projectDiscussion");
        var $projectFile = $("#projectFile");

        /*
         * 滑动条当前的left属性值，
         * 在$content.mouseleave方法中用于固定滑动条位置，
         * 防止鼠标指针移开后滑动条回到初始位置
         */
        var currentElePL;

        if (listType == 1 || listType == 4) {
            //第二个链接
            /*初始化滑动条位置*/
            var aArray = $(".menu-li a");
            var $second = $(aArray[1]), // 获取第二个链接元素
                liPL = $second.position().left, // 获取第二个链接元素的left属性值
                $slider = $("div.line"), //获取滑动条
                $content = $(".menu-line");//获取导航条容器
            $allEle = $(".menu-ul li.menu-li a"); //获取所有链接

            //进入页面默认选中第一个链接，设置它的字体颜色
            $allEle.css("color", "black");
            $second.css("color", "rgb(97, 188, 247)");
            $slider.stop(true, true).animate({
                "left": liPL
            });
        }
        else if (listType == 0 || listType == 3) {
            /*初始化滑动条位置*/
            var $first = $(".menu-li a:first"), // 获取第一个链接元素
                liPL = $first.position().left, // 获取第一个链接元素的left属性值
                $slider = $("div.line"), //获取滑动条
                $content = $(".menu-line");//获取导航条容器
            $allEle = $(".menu-ul li.menu-li a"); //获取所有链接
            //进入页面默认选中第一个链接，设置它的字体颜色
            $allEle.css("color", "black");
            $(".menu-li a:first").css("color", "rgb(97, 188, 247)");
            $slider.stop(true, true).animate({
                "left": liPL
            });
        }
        else {
            /*初始化滑动条位置*/
            var $last = $(".menu-li a:last"), // 获取第一个链接元素
                liPL = $last.position().left, // 获取第一个链接元素的left属性值
                $slider = $("div.line"), //获取滑动条
                $content = $(".menu-line");//获取导航条容器
            $allEle = $(".menu-ul li.menu-li a"); //获取所有链接

            //进入页面默认选中第一个链接，设置它的字体颜色
            $allEle.css("color", "black");
            $(".menu-li a:last").css("color", "rgb(97, 188, 247)");
            $slider.stop(true, true).animate({
                "left": liPL
            });
        }


        /*滑动条跟随鼠标指针移动*/
        //手机端要变成点击事件而不是鼠标事件
        $allEle.click(function () {
            $currentEle = $(this);
            currentElePL = $currentEle.position().left;

            //切换展示列表区域

            //先判断当前页是哪个页面

            //任务首页
            if (window.location.href.indexOf("index.html")) {
                if ($currentEle.text() == "我负责的") {
                    if (listType != 0) {
                        $chargeDiv.css("display", "block");
                        $initiateDiv.css("display", "none");
                        $followDiv.css("display", "none");
                        listType = 0;
                        projectTaskListWithMe(user_id);
                        $(".task-complete-p").text("查看已完成任务");
                        $(".complete-row").css("transform", "rotate(0deg)");
                        $(".task-div").show();
                        $(".show-hide-task").hide();
                    }

                } else if ($currentEle.text() == "我发起的") {
                    if (listType != 1) {
                        $chargeDiv.css("display", "none");
                        $initiateDiv.css("display", "block");
                        $followDiv.css("display", "none");
                        listType = 1;
                        projectTaskListWithMe(user_id);
                        $(".task-complete-p").text("查看已完成任务");
                        $(".complete-row").css("transform", "rotate(0deg)");
                        $(".task-div").show();
                        $(".show-hide-task").hide();
                    }

                } else if ($currentEle.text() == "我关注的") {
                    if (listType != 2) {
                        $chargeDiv.css("display", "none");
                        $initiateDiv.css("display", "none");
                        $followDiv.css("display", "block");
                        listType = 2;
                        projectTaskListWithMe(user_id);
                        $(".task-div").hide();
                        $(".show-hide-task").hide();
                    }
                }

            }

            //项目详情
            if (window.location.href.indexOf("projectDetail.html")) {

                if ($currentEle.text() == "任务") {
                    if (listType != 3) {
                        $projectTask.css("display", "block");
                        $projectDiscussion.css("display", "none");
                        $projectFile.css("display", "none");
                        projectTaskList(project_id, 0, -1);
                        listType = 3;
                        $(".task-complete-p").text("查看已完成任务");
                        $(".complete-row").css("transform", "rotate(0deg)");
                        $(".project-li").remove();
                        $(".projectComment-li").remove();
                    }

                } else if ($currentEle.text() == "讨论") {
                    if (listType != 4) {
                        $projectTask.css("display", "none");
                        $projectDiscussion.css("display", "block");
                        $projectFile.css("display", "none");
                        projectDynamicList(0, project_id);
                        listType = 4;
                        $(".projectTaskCharge-li").remove();
                        $(".project-li").remove();
                    }

                } else if ($currentEle.text() == "文件") {
                    if (listType != 5) {
                        $projectTask.css("display", "none");
                        $projectDiscussion.css("display", "none");
                        $projectFile.css("display", "block");
                        fileLists(project_id, 0);
                        listType = 5;
                        $(".projectTaskCharge-li").remove();
                        $(".projectComment-li").remove();
                    }
                }
            }


            //将其他未选中链接的文字颜色重置为黑色
            $allEle.css("color", "black");
            $currentEle.css("color", "rgb(97, 188, 247)");
            $slider.stop(true, true).animate({
                "left": currentElePL
            }, "fast");
        });

        /*鼠标移开后固定滑动条位置*/
        $content.mouseleave(function (PL) {
            PL = currentElePL;
            $slider.stop(true, true).animate({
                "left": PL
            }, "fast");
        });
    }

    //获取动态列表
    function dynamicLists(user_id, timestamp) {
        var url = apiurl + "/projects/task/dynamic";
        var urlData = {
            group_id: group_id,
            user_id: user_id,
            token: token,
            timestamp: timestamp
        }
        $.ajax({
            url: url,
            type: "GET",
            data: urlData,
            beforeSend: function () {
                if (timestamp == 0) {
                    $(".loading").show();
                }
            },
            complete: function () {
                if (timestamp == 0) {
                    $(".loading").hide();
                }
            },
            success: function (d) {
                stop = true;
                if (d.msg == 202) {
                    $(".token_iframe").show();
                }
                else {
                    var comData = d;
                    if (d.code == undefined) {
                        comData = JSON.parse(d);
                    }
                    if (comData.code == 0) {
                        var data = JSON.parse(d).data.projectStatusVos;
                        userArray = JSON.parse(d).data.userInfos;
                        if (timestamp == 0) {
                            //首次获取数据
                            if (data == "") {
                                $(".noTask-div").show();
                                stop = false;
                            } else {
                                $(".noTask-div").hide();
                                configUl(data, 1);
                            }
                        }
                        else {
                            if (data == "") {
                                stop = false;
                            } else {
                                if (lastTime == data[0].time) {
                                    configUl(data, 0);
                                }
                                else {
                                    configUl(data, 1);
                                }
                            }
                        }
                    }
                    else {
                        $(".task-alert").text("Error").show().fadeOut(2000);
                    }
                }
            },
            error: function (d) {
                stop = true;
                $(".task-alert").text("网络不给力").show().fadeOut(2000);
            }
        })
    }

    function configUl(data, configDiv) {
        for (var i = 0; i < data.length; i++) {
            var time = data[i].time;
            if (time == lastTime) {
                configDiv = 0;
            }
            else {
                configDiv = 1;
            }
            lastTime = time;
            var ulId = "dynamic-ul" + time;
            var array = data[i].projectStatus;
            initDate(new Date());
            if (time == year_mon_day_en) {
                time = "今天";
            }
            if (configDiv == 1) {
                var html = "<div class='dynamic-div'>" +
                    "<div class='dynamic-date'>" +
                    "<div class='behindDiv'>" +
                    "<div class='firstDiv'></div>" +
                    "<div class='secondDiv'></div></div>" +
                    "<div class='frontDiv'>" +
                    "<p class='date'>" + time + "</p></div></div>" +
                    "<ul class='dynamic-ul' id='" + ulId + "'></ul>" +
                    "<div class='clear'></div></div>";
                $(".dynamicContent-div").append(html);
            }
            for (var j = 0; j < array.length; j++) {
                lastTimeStamp = array[j].create_timestamp;
                var projectName = array[j].project_title;
                initDate(new Date(array[j].create_timestamp));
                var statusString = "";
                var valString = "";
                var userName = "";
                var avatar = "";
                var qyxBindId = '';
                for (key in userArray) {
                    if (array[j].user_id == userArray[key].id) {
                        userName = userArray[key].name;
                        avatar = userArray[key].avatar;
                        qyxBindId = userArray[key].qyxBindId;
                    }
                }
                var img_url;
                //if (avatar == "") {
                    var color = avatar_getColor(qyxBindId);
                    img_url = "<div class='user_pic_div_dy' style='background-color: " + color + "'>" + avatar_name(userName) + "</div>"
                //} else {
                if (avatar.length > 0) {
                    var src = avatar_getUrl(localStorage.user_id, localStorage.token, avatar);
                    img_url += "<img src='" + src + "' class='user_pic_dy' hidden>";
                }
                if (array[j].status == 0) {
                    //文字评论
                    statusString = userName + " 回复了任务";
                    valString = array[j].comment_body;
                }
                else if (array[j].status == 1) {
                    //带文件评论
                    statusString = userName + " 回复了任务";
                    valString = "详情见附件";
                }
                else if (array[j].status == 2) {
                    //创建
                    statusString = userName + " 创建了任务";
                }
                else if (array[j].status == 3) {
                    //指派
                    for (key in userArray) {
                        if (array[j].assign_id == userArray[key].id) {
                            statusString = userName + " 把任务指派给了 " + userArray[key].name;
                        }
                    }
                }
                else if (array[j].status == 4) {
                    //修改
                    statusString = userName + " 修改了任务";
                }
                else if (array[j].status == 5) {
                    //删除
                    statusString = userName + " 删除了任务";
                }
                else if (array[j].status == 6) {
                    //完成
                    statusString = userName + " 完成了任务";
                }
                else if (array[j].status == 7) {
                    //移动
                    statusString = userName + " 移动了任务";
                }
                else if (array[j].status == 8) {
                    //移动
                    statusString = userName + " 撤销了任务完成";
                }
                var liHtml = '<li class="dynamic-li" project_task_id="' + array[j].project_task_id + '" del="' + array[j].del_flag + '">' +
                    '<div class="top-span">' +
                    '<hr class="line" />' +
                    '<p class="projectName">项目:' + projectName + '</p></div>' +
                    '<div class="left-span">' +
                    //'<img class="photo" src="'+avatar+'" />'+
                    img_url +
                    '<p class="time">' + hours_min + '</p></div>' +
                    '<div class="right-span">' +
                    //'<p class="name">'+statusString+'</p>'+
                    '<p class="message">' + statusString + '</p>' +
                    '<p class="content">' + array[j].project_task_name + '</p>' +
                    '<p class="attached">' + valString + '</p></div></li>';
                $("#" + ulId + "").append(liHtml);

            }
            showAvatar(".user_pic_dy");
        }

        $(".dynamicContent-div").show();
    }

    // 前往任务详情
    $(document).on('click', '.dynamic-li', function () {
        var project_task_id = $(this).attr("project_task_id");
        var del_flag = $(this).attr("del");
        if (del_flag == 0) {
            window.location.href = "html/task/taskDetail.html?id=" + project_task_id + "&detailBack=2";
        }
        else {
            $(".task-alert").text("该任务已被删除").show().fadeOut(2000);
        }
    })

    //上传项目文件
    function uploadFile(user_id, project_id, file_name, file_url) {
        var url = apiurl + "/projects/file";
        var data = {
            project_id: project_id,
            user_id: user_id,
            project_file_name: file_name,
            project_url: file_url
        }
        $.ajax({
            url: url,
            type: "POST",
            data: data,
            beforeSend: function () {
                $(".change-p").text("正在上传文件");
                $(".change-alert").show();
            },
            complete: function (d) {
                $(".change-alert").hide();
            },
            success: function (d) {
                var data = d;
                if (d.code == undefined) {
                    data = JSON.parse(d);
                }
                if (data.code == 0) {
                    if ($(".noTask-div").is(":visible")) {
                        $(".content-div").show();
                        $(".noTask-div").hide();
                    }

                    var arr = file_name.split(".");
                    var type = '';
                    var imgUrl = '';
                    if (arr[arr.length - 1] == "jpg" || arr[arr.length - 1] == "png" || arr[arr.length - 1] == "jpeg") {
                        type = "project-pic-img";
                        imgUrl = '../../images/task/pic.png';
                    }
                    else {
                        type = "project-img";
                        imgUrl = '../../images/project/word.png';
                    }

                    $("#projectFile-ul").prepend("<li class='project-li' file_data_down = '" + file_url + "' file_name_down='" + file_name + "'>" +
                        "<img class='" + type + "' src='" + imgUrl + "' />" +
                        "<span class='project-span'>" + file_name + "</span>" +
                        "<hr class='bottomLine' /></li>")
                }
                else {
                    $(".project-alert").text("Error").show().fadeOut(2000);
                }

                $(document).on('click', '.project-li', function () {
                    $(".call_back_down_p").show();
                    file_data_down = $(this).attr("file_data_down");
                    file_name_down = $(this).attr("file_name_down");
                })
            },
            error: function (d) {
                $(".project-alert").text("网络不给力").show().fadeOut(2000);
            }
        })
    }

    //获取项目详情
    function projectDetail(project_id) {
        var url = apiurl + "/projects/" + project_id + "";
        var urlData = {
            user_id: user_id,
            token: token
        }
        $.ajax({
            url: url,
            type: "GET",
            data: urlData,
            beforeSend: function (d) {
                $(".loading").show();
                $(".content-div").hide();
            },
            success: function (d) {
                if (d.msg == 202) {
                    $(".token_iframe").show();
                }
                else {
                    var comData = d;
                    if (d.code == undefined) {
                        comData = JSON.parse(d);
                    }
                    if (comData.code == 0) {
                        var data = JSON.parse(d).data;
                        var title = data.project_title;
                        $(".title").html(title);
                        projectMembers = data.userCallbackString;
                        if (projectCommentBack == 1) {
                            //项目讨论列表
                            //项目详情
                            var $projectTask = $("#projectTask");
                            var $projectDiscussion = $("#projectDiscussion");
                            var $projectFile = $("#projectFile");
                            $projectTask.css("display", "none");
                            $projectDiscussion.css("display", "block");
                            $projectFile.css("display", "none");
                            projectDynamicList(0, project_id);
                        }
                        else {
                            projectTaskList(project_id, 0, -1);
                        }
                    }
                    else {
                        $(".project-alert").text("Error").show().fadeOut(2000);
                    }
                }
            },
            error: function (d) {
                $(".loading").hide();
                $(".project-alert").text("网络不给力").show().fadeOut(2000);
            }
        })
    }

    //获取与我有关的未完成任务列表
    function projectTaskListWithMe(user_id) {
        //listType 0我负责的 1我发起的 2我关注的
        $(document).on("click", ".charge-li", function () {
            window.location.href = "html/task/taskDetail.html?id=" + $(this).attr("project_task_id") + "&detailBack=0&taskListBack=" + listType;
        });

        var url = apiurl + "/projects/task/userlist";
        var urldata = {
            group_id: group_id,
            user_id: user_id,
            token: token,
            listType: listType,
            timestamp: new Date().getTime()
        }

        $.ajax({
            url: url,
            type: "GET",
            data: urldata,
            beforeSend: function (d) {
                $(".taskLoading").show();
                $(".content-div").hide();
                $(".noDataContent-div").hide();
            },
            complete: function (d) {
                $(".taskLoading").hide();
            },
            success: function (d) {
                if (d.msg == 202) {
                    $(".token_iframe").show();
                }
                else {
                    var comData = d;
                    if (d.code == undefined) {
                        comData = JSON.parse(d);
                    }
                    if (comData.code == 0) {
                        var data = JSON.parse(d).data;
                        tasksMembers = data.userCallbackString;
                        var total = data.total;
                        if (total == 0) {
                            $(".content-div").hide();
                            $(".noDataContent-div").show();
                            if (listType == 0) {
                                $(".task-tip").text("你还未创建任务");
                            } else if (listType == 1) {
                                $(".task-tip").text("你还未发起任务");
                            }
                            else if (listType == 2) {
                                $(".task-tip").text("你还未关注任务");
                            }
                        } else {
                            $(".content-div").show();
                            $(".noDataContent-div").hide();

                            if (listType == 0) {
                                //我负责的
                                var delayData = data.delay;
                                var todayData = data.today;
                                var laterData = data.later;
                                //标题
                                //$("#delay-count").text("(" + delayData.length + ")");
                                //$("#today-count").text("(" + todayData.length + ")");
                                $("#later-count").text("(" + laterData.length + ")");
                                //内容主体
                                var html = '';
                                var todayCount = 0;
                                if (delayData.length > 0) {
                                    //有延期任务
                                    for (var i = 0; i < delayData.length; i++) {
                                        var task = delayData[i];
                                        var taskDate = new Date(task.deadline);
                                        taskDate.setHours(0);
                                        taskDate.setMinutes(0);
                                        taskDate.setSeconds(0);
                                        taskDate.setMilliseconds(0);
                                        var taskDateTime = taskDate.getTime() / 1000;
                                        var today = new Date();
                                        today.setHours(0);
                                        today.setMinutes(0);
                                        today.setSeconds(0);
                                        today.setMilliseconds(0);
                                        var todayTime = today.getTime() / 1000;
                                        if (taskDateTime == todayTime) {
                                            //今天的任务
                                            todayCount++;
                                            todayData.push(task);
                                        }
                                        else {
                                            //设置时间
                                            initDate(new Date(task.deadline));
                                            if (i == delayData.length - 1) {
                                                html += '<li class="charge-li" project_task_id="' + task.project_task_id + '" >' +
                                                    '<p class="taskName">' + task.project_task_name + '</p>' +
                                                    '<span class="secondSpan">' +
                                                    '<img class="timeImg" src="../task/images/task/clockRed.png" />' +
                                                    '<span class="deadline">' + mon_day + '</span>' +
                                                    '<img class="projectImg" src="../task/images/task/projectImg.png" />' +
                                                    '<span class="projectName">' + task.project_title + '</span>' +
                                                    '</span>' +
                                                    '<div class="whiteLine"></div> ' +
                                                    //'<hr class="bottomLine" />' +
                                                    '</li>';
                                            }
                                            else {
                                                html += '<li class="charge-li" project_task_id="' + task.project_task_id + '" >' +
                                                    '<p class="taskName">' + task.project_task_name + '</p>' +
                                                    '<span class="secondSpan">' +
                                                    '<img class="timeImg" src="../task/images/task/clockRed.png" />' +
                                                    '<span class="deadline">' + mon_day + '</span>' +
                                                    '<img class="projectImg" src="../task/images/task/projectImg.png" />' +
                                                    '<span class="projectName">' + task.project_title + '</span>' +
                                                    '</span>' +
                                                    '<div class="liLine"></div> ' +
                                                    //'<hr class="bottomLine" />' +
                                                    '</li>';
                                            }
                                        }
                                    }
                                    $("#delay").empty().append(html);

                                    html = '';
                                }
                                var delayCount = delayData.length - todayCount;
                                $("#delay-count").text("(" + delayCount + ")");
                                $("#today-count").text("(" + todayData.length + ")");
                                if (todayData.length > 0) {
                                    //有今天任务
                                    for (var i = 0; i < todayData.length; i++) {
                                        var task = todayData[i];
                                        //设置时间
                                        initDate(new Date(task.deadline));
                                        if (i == todayData.length - 1) {
                                            html += '<li class="charge-li" project_task_id="' + task.project_task_id + '" >' +
                                                '<p class="taskName">' + task.project_task_name + '</p>' +
                                                '<span class="secondSpan">' +
                                                '<img class="timeImg" src="../task/images/task/clockRed.png" />' +
                                                '<span class="deadline">' + mon_day + '</span>' +
                                                '<img class="projectImg" src="../task/images/task/projectImg.png" />' +
                                                '<span class="projectName">' + task.project_title + '</span>' +
                                                '</span>' +
                                                '<div class="whiteLine"></div> ' +
                                                //'<hr class="bottomLine" />' +
                                                '</li>';
                                        }
                                        else {
                                            html += '<li class="charge-li" project_task_id="' + task.project_task_id + '" >' +
                                                '<p class="taskName">' + task.project_task_name + '</p>' +
                                                '<span class="secondSpan">' +
                                                '<img class="timeImg" src="../task/images/task/clockRed.png" />' +
                                                '<span class="deadline">' + mon_day + '</span>' +
                                                '<img class="projectImg" src="../task/images/task/projectImg.png" />' +
                                                '<span class="projectName">' + task.project_title + '</span>' +
                                                '</span>' +
                                                '<div class="liLine"></div> ' +
                                                //'<hr class="bottomLine" />' +
                                                '</li>';
                                        }
                                    }
                                    $("#today").empty().append(html);
                                    html = '';
                                }
                                if (laterData.length > 0) {
                                    //有以后任务
                                    for (var i = 0; i < laterData.length; i++) {
                                        var task = laterData[i];
                                        //设置时间
                                        initDate(new Date(task.deadline));
                                        if (i == laterData.length - 1) {
                                            html += '<li class="charge-li" project_task_id="' + task.project_task_id + '" >' +
                                                '<p class="taskName">' + task.project_task_name + '</p>' +
                                                '<span class="secondSpan">' +
                                                '<img class="timeImg" src="../task/images/task/clockWhite.png" />' +
                                                '<span class="afterdeadline">' + mon_day + '</span>' +
                                                '<img class="projectImg" src="../task/images/task/projectImg.png" />' +
                                                '<span class="projectName">' + task.project_title + '</span>' +
                                                '</span>' +
                                                '<div class="whiteLine"></div> ' +
                                                //'<hr class="bottomLine" />' +
                                                '</li>';
                                        }
                                        else {
                                            html += '<li class="charge-li" project_task_id="' + task.project_task_id + '" >' +
                                                '<p class="taskName">' + task.project_task_name + '</p>' +
                                                '<span class="secondSpan">' +
                                                '<img class="timeImg" src="../task/images/task/clockWhite.png" />' +
                                                '<span class="afterdeadline">' + mon_day + '</span>' +
                                                '<img class="projectImg" src="../task/images/task/projectImg.png" />' +
                                                '<span class="projectName">' + task.project_title + '</span>' +
                                                '</span>' +
                                                '<div class="liLine"></div> ' +
                                                //'<hr class="bottomLine" />' +
                                                '</li>';
                                        }
                                    }
                                    $("#later").empty().append(html);
                                    html = '';
                                }
                            } else if (listType == 1) {
                                var list = data.list;
                                //内容主体
                                var html = '';
                                if (list.length > 0) {
                                    for (var i = 0; i < list.length; i++) {
                                        var task = list[i];
                                        var avatar = '';
                                        var name = '';
                                        var qyxBindId = '';
                                        for (key in tasksMembers) {
                                            if (tasksMembers[key].id == task.response_id) {
                                                avatar = tasksMembers[key].avatar;
                                                name = tasksMembers[key].name;
                                                qyxBindId = tasksMembers[key].qyxBindId;
                                            }
                                        }
                                        var img_url;
                                        //if (avatar == "") {
                                            var color = avatar_getColor(qyxBindId);
                                            img_url = "<div class='user_pic_div' style='background-color: " + color + "'>" + avatar_name(name) + "</div>"
                                        //} else {
                                        if (avatar.length > 0) {
                                            var src = avatar_getUrl(localStorage.user_id, localStorage.token, avatar);
                                            img_url += "<img src='" + src + "' class='user_pic' hidden>";
                                        }
                                        //设置时间
                                        var taskDate = new Date(task.deadline);
                                        taskDate.setHours(0);
                                        taskDate.setMinutes(0);
                                        taskDate.setSeconds(0);
                                        taskDate.setMilliseconds(0);
                                        var today = new Date();
                                        today.setHours(24);
                                        today.setMinutes(0);
                                        today.setSeconds(0);
                                        today.setMilliseconds(0);
                                        initDate(new Date(task.deadline));
                                        if (taskDate < today) {
                                            //延期
                                            if (i == list.length - 1) {
                                                html += '<li class="charge-li" project_task_id="' + task.project_task_id + '" >' +
                                                    img_url +
                                                    '<p class="taskName">' + task.project_task_name + '</p>' +
                                                    //'<img class="avatarImg" src="'+avatar+'" />'+
                                                    '<span class="secondSpan">' +
                                                    '<img class="timeImg" src="../task/images/task/clockRed.png" />' +
                                                    '<span class="deadline">' + mon_day + '</span>' +
                                                    '<img class="projectImg" src="../task/images/task/projectImg.png" />' +
                                                    '<span class="projectName">' + task.project_title + '</span>' +
                                                    '</span>' +
                                                    '<div class="whiteLine"></div> ' +
                                                    //'<hr class="bottomLine" />' +
                                                    '</li>';
                                            }
                                            else {
                                                html += '<li class="charge-li" project_task_id="' + task.project_task_id + '" >' +
                                                    img_url +
                                                    '<p class="taskName">' + task.project_task_name + '</p>' +
                                                    //'<img class="avatarImg" src="'+avatar+'" />'+
                                                    '<span class="secondSpan">' +
                                                    '<img class="timeImg" src="../task/images/task/clockRed.png" />' +
                                                    '<span class="deadline">' + mon_day + '</span>' +
                                                    '<img class="projectImg" src="../task/images/task/projectImg.png" />' +
                                                    '<span class="projectName">' + task.project_title + '</span>' +
                                                    '</span>' +
                                                    '<div class="liLine"></div> ' +
                                                    //'<hr class="bottomLine" />' +
                                                    '</li>';
                                            }
                                        }
                                        else {
                                            if (i == list.length - 1) {
                                                html += '<li class="charge-li" project_task_id="' + task.project_task_id + '" >' +
                                                    img_url +
                                                    '<p class="taskName">' + task.project_task_name + '</p>' +
                                                    //'<img class="avatarImg" src="'+avatar+'" />'+
                                                    '<span class="secondSpan">' +
                                                    '<img class="timeImg" src="../task/images/task/clockWhite.png" />' +
                                                    '<span class="afterdeadline">' + mon_day + '</span>' +
                                                    '<img class="projectImg" src="../task/images/task/projectImg.png" />' +
                                                    '<span class="projectName">' + task.project_title + '</span>' +
                                                    '</span>' +
                                                    '<div class="whiteLine"></div> ' +
                                                    //'<hr class="bottomLine" />' +
                                                    '</li>';
                                            }
                                            else {
                                                html += '<li class="charge-li" project_task_id="' + task.project_task_id + '" >' +
                                                    img_url +
                                                    '<p class="taskName">' + task.project_task_name + '</p>' +
                                                    //'<img class="avatarImg" src="'+avatar+'" />'+
                                                    '<span class="secondSpan">' +
                                                    '<img class="timeImg" src="../task/images/task/clockWhite.png" />' +
                                                    '<span class="afterdeadline">' + mon_day + '</span>' +
                                                    '<img class="projectImg" src="../task/images/task/projectImg.png" />' +
                                                    '<span class="projectName">' + task.project_title + '</span>' +
                                                    '</span>' +
                                                    '<div class="liLine"></div> ' +
                                                    //'<hr class="bottomLine" />' +
                                                    '</li>';
                                            }
                                        }

                                    }
                                    $("#public").empty().append(html);


                                    showAvatar(".user_pic");
                                    html = '';
                                }
                            } else if (listType == 2) {
                                var list = data.list;
                                //内容主体
                                var html = '';
                                if (list.length > 0) {
                                    for (var i = 0; i < list.length; i++) {
                                        var task = list[i];
                                        var avatar = '';
                                        var name = '';
                                        var qyxBindId = '';
                                        for (key in tasksMembers) {
                                            if (tasksMembers[key].id == task.response_id) {
                                                avatar = tasksMembers[key].avatar;
                                                name = tasksMembers[key].name;
                                                qyxBindId = tasksMembers[key].qyxBindId;
                                            }
                                        }
                                        var status = '';
                                        var img_url;
                                        //if (avatar == "") {
                                            var color = avatar_getColor(qyxBindId);
                                            img_url = "<div class='user_pic_div' style='background-color: " + color + "'>" + avatar_name(name) + "</div>"
                                        //} else {
                                        if (avatar.length > 0) {
                                            var src = avatar_getUrl(localStorage.user_id, localStorage.token, avatar);
                                            img_url += "<img src='" + src + "' class='user_pic' hidden>";
                                        }
                                        //设置时间
                                        var taskDate = new Date(task.deadline);
                                        taskDate.setHours(0);
                                        taskDate.setMinutes(0);
                                        taskDate.setSeconds(0);
                                        taskDate.setMilliseconds(0);
                                        var today = new Date();
                                        today.setHours(24);
                                        today.setMinutes(0);
                                        today.setSeconds(0);
                                        today.setMilliseconds(0);
                                        initDate(new Date(task.deadline));
                                        if (taskDate < today) {
                                            //延期
                                            if (i == list.length - 1) {
                                                if (task.status == 1) {
                                                    status = '完成';
                                                    html += '<li class="charge-li" style="float: left" project_task_id="' + task.project_task_id + '" >' +
                                                        img_url +
                                                        '<img class="taskFinishImg" src="images/task/complete.png"/> ' +
                                                        '<p  class="taskFinishName" style="float: left">' + task.project_task_name + '</p>' +
                                                        '<span class="secondSpan" style="float: left">' +
                                                        '<img class="timeImg" src="../task/images/task/clockWhite.png" />' +
                                                        '<span class="finishdeadline">' + mon_day + status + '</span>' +
                                                        '<img class="projectImg" src="../task/images/task/projectImg.png" />' +
                                                        '<span class="projectName">' + task.project_title + '</span>' +
                                                        '</span>' +
                                                        '<div class="whiteLine" style="float: left"></div> ' +
                                                        '</li>';
                                                }
                                                else {
                                                    html += '<li class="charge-li" style="float: left" project_task_id="' + task.project_task_id + '" >' +
                                                        img_url +
                                                        '<p class="taskName" style="float: left">' + task.project_task_name + '</p>' +
                                                        '<span class="secondSpan" style="float: left">' +
                                                        '<img class="timeImg" src="../task/images/task/clockRed.png" />' +
                                                        '<span class="deadline">' + mon_day + status + '</span>' +
                                                        '<img class="projectImg" src="../task/images/task/projectImg.png" />' +
                                                        '<span class="projectName">' + task.project_title + '</span>' +
                                                        '</span>' +
                                                        '<div class="whiteLine" style="float: left"></div> ' +
                                                        '</li>';
                                                }
                                            }
                                            else {
                                                if (task.status == 1) {
                                                    status = '完成';
                                                    html += '<li class="charge-li" style="float: left" project_task_id="' + task.project_task_id + '" >' +
                                                        img_url +
                                                        '<img class="taskFinishImg" src="images/task/complete.png"/> ' +
                                                        '<p  class="taskFinishName" style="float: left">' + task.project_task_name + '</p>' +
                                                        '<span class="secondSpan" style="float: left">' +
                                                        '<img class="timeImg" src="../task/images/task/clockWhite.png" />' +
                                                        '<span class="finishdeadline">' + mon_day + status + '</span>' +
                                                        '<img class="projectImg" src="../task/images/task/projectImg.png" />' +
                                                        '<span class="projectName">' + task.project_title + '</span>' +
                                                        '</span>' +
                                                        '<div class="liLine" style="float: left"></div> ' +
                                                        '</li>';
                                                }
                                                else {
                                                    html += '<li class="charge-li" style="float: left" project_task_id="' + task.project_task_id + '" >' +
                                                        img_url +
                                                        '<p class="taskName" style="float: left">' + task.project_task_name + '</p>' +
                                                        '<span class="secondSpan" style="float: left">' +
                                                        '<img class="timeImg" src="../task/images/task/clockRed.png" />' +
                                                        '<span class="deadline">' + mon_day + status + '</span>' +
                                                        '<img class="projectImg" src="../task/images/task/projectImg.png" />' +
                                                        '<span class="projectName">' + task.project_title + '</span>' +
                                                        '</span>' +
                                                        '<div class="liLine" style="float: left"></div> ' +
                                                        '</li>';
                                                }
                                            }
                                        }
                                        else {
                                            if (i == list.length - 1) {
                                                if (task.status == 1) {
                                                    status = '完成';
                                                    html += '<li class="charge-li" style="float: left" project_task_id="' + task.project_task_id + '" >' +
                                                        img_url +
                                                        '<img class="taskFinishImg" src="images/task/complete.png"/> ' +
                                                        '<p  class="taskFinishName" style="float: left">' + task.project_task_name + '</p>' +
                                                        '<span class="secondSpan" style="float: left">' +
                                                        '<img class="timeImg" src="../task/images/task/clockWhite.png" />' +
                                                        '<span class="finishdeadline">' + mon_day + status + '</span>' +
                                                        '<img class="projectImg" src="../task/images/task/projectImg.png" />' +
                                                        '<span class="projectName">' + task.project_title + '</span>' +
                                                        '</span>' +
                                                        '<div class="whiteLine" style="float: left"></div> ' +
                                                        '</li>';
                                                }
                                                else {
                                                    html += '<li class="charge-li" style="float: left" project_task_id="' + task.project_task_id + '" >' +
                                                        img_url +
                                                        '<p class="taskName" style="float: left">' + task.project_task_name + '</p>' +
                                                        '<span class="secondSpan" style="float: left">' +
                                                        '<img class="timeImg" src="../task/images/task/clockRed.png" />' +
                                                        '<span class="deadline">' + mon_day + status + '</span>' +
                                                        '<img class="projectImg" src="../task/images/task/projectImg.png" />' +
                                                        '<span class="projectName">' + task.project_title + '</span>' +
                                                        '</span>' +
                                                        '<div class="whiteLine" style="float: left"></div> ' +
                                                        '</li>';
                                                }
                                            }
                                            else {
                                                if (task.status == 1) {
                                                    status = '完成';
                                                    html += '<li class="charge-li" style="float: left" project_task_id="' + task.project_task_id + '" >' +
                                                        img_url +
                                                        '<img class="taskFinishImg" src="images/task/complete.png"/> ' +
                                                        '<p  class="taskFinishName" style="float: left">' + task.project_task_name + '</p>' +
                                                        '<span class="secondSpan" style="float: left">' +
                                                        '<img class="timeImg" src="../task/images/task/clockWhite.png" />' +
                                                        '<span class="finishdeadline">' + mon_day + status + '</span>' +
                                                        '<img class="projectImg" src="../task/images/task/projectImg.png" />' +
                                                        '<span class="projectName">' + task.project_title + '</span>' +
                                                        '</span>' +
                                                        '<div class="liLine" style="float: left"></div> ' +
                                                        '</li>';
                                                }
                                                else {
                                                    html += '<li class="charge-li" style="float: left" project_task_id="' + task.project_task_id + '" >' +
                                                        img_url +
                                                        '<p class="taskName" style="float: left">' + task.project_task_name + '</p>' +
                                                        '<span class="secondSpan" style="float: left">' +
                                                        '<img class="timeImg" src="../task/images/task/clockWhite.png" />' +
                                                        '<span class="afterdeadline">' + mon_day + status + '</span>' +
                                                        '<img class="projectImg" src="../task/images/task/projectImg.png" />' +
                                                        '<span class="projectName">' + task.project_title + '</span>' +
                                                        '</span>' +
                                                        '<div class="liLine" style="float: left"></div> ' +
                                                        '</li>';
                                                }
                                            }
                                        }

                                    }
                                    $("#follower").empty().append(html);
                                    html = '';

                                    showAvatar(".user_pic");
                                }
                            }
                        }
                    }
                    else {
                        $(".task-alert").text("Error").show().fadeOut(2000);
                    }
                }
            },
            error: function (d) {
                $(".task-alert").text("网络不给力").show().fadeOut(2000);
            }
        })
    }

    // 已完成任务列表展开／折叠状态切换
    function toggleDisplay() {
        var $taskDiv = $(".task");
        var $completeUl = $(".task-div #complete");
        var $completeP = $(".task-complete-p");

        //默认隐藏已完成任务列表
        $completeUl.css("display", "none");

        $taskDiv.click(function () {
            $completeUl.fadeToggle();
            if ($completeP.text() == "查看已完成任务") {
                $completeP.text("隐藏已完成任务");
                $(".complete-row").css("transform", "rotate(90deg)");
                if (tabbarCount == 1) {
                    projectTaskList(project_id, 0, 1);
                }
                else {
                    finishProjectTaskListWithMe();
                }

            } else {
                $completeP.text("查看已完成任务");
                $(".complete-row").css("transform", "rotate(0deg)");
                $(".show-hide-task").hide();
            }
        });
    }


    //获取已完成任务列表
    function finishProjectTaskListWithMe() {
        var timestamp = 0;
        //listType 0我负责的 1我发起的 2我关注的
        var url = apiurl + "/projects/task/userlist/complete";
        var urldata = {
            group_id: group_id,
            user_id: user_id,
            token: token,
            listType: listType,
            timestamp: timestamp
        }
        $.ajax({
            url: url,
            type: "GET",
            data: urldata,
            success: function (d) {
                if (d.msg == 202) {
                    $(".token_iframe").show();
                }
                else {
                    var comData = d;
                    if (d.code == undefined) {
                        comData = JSON.parse(d);
                    }
                    if (comData.code == 0) {
                        var list = JSON.parse(d).data.list;
                        tasksMembers = JSON.parse(d).data.userCallbackString;
                        var html = "";
                        if (list.length <= 0) {
                            $(".task-alert").text("当前无已完成任务").show().fadeOut(2000);
                        }
                        for (var i = 0; i < list.length; i++) {
                            var task = list[i];
                            var avatar = '';
                            var name = '';
                            var qyxBindId = '';
                            for (key in tasksMembers) {
                                if (tasksMembers[key].id == task.response_id) {
                                    avatar = tasksMembers[key].avatar;
                                    name = tasksMembers[key].name;
                                    qyxBindId = tasksMembers[key].qyxBindId;
                                }
                            }
                            var img_url;
                            //if (avatar == "") {
                                var color = avatar_getColor(qyxBindId);
                                img_url = "<div class='user_pic_div' style='background-color: " + color + "'>" + avatar_name(name) + "</div>"
                            //} else {
                            if (avatar.length > 0){
                                var src = avatar_getUrl(localStorage.user_id, localStorage.token, avatar);
                                img_url += "<img src='" + src + "' class='user_pic' hidden>";
                            }
                            var task_name = task.project_task_name;
                            var complete_timestamp = new Date(task.complete_timestamp);
                            initDate(complete_timestamp);
                            if (i == list.length - 1) {
                                if (listType == 0) {
                                    html += "<li class='charge-li show-hide-task' update_timestamp='" + task.update_timestamp + "' project_task_id='" + task.project_task_id + "' >" +
                                        "<p class='taskName'>" + task_name + "</p>" +
                                        "<span class='secondSpan'>" +
                                        "<img class='timeImg' src='../task/images/task/clockWhite.png' />" +
                                        "<span class='completeTime'>" + mon_day + " 完成</span>" +
                                        "</span>" +
                                        '<div class="whiteLine"></div> ' +
                                        "</li>";
                                }
                                else {
                                    html += "<li class='charge-li show-hide-task' update_timestamp='" + task.update_timestamp + "' project_task_id='" + task.project_task_id + "' >" +
                                        img_url +
                                        "<p class='taskName'>" + task_name + "</p>" +
                                        "<span class='secondSpan'>" +
                                        "<img class='timeImg' src='../task/images/task/clockWhite.png' />" +
                                        "<span class='completeTime'>" + mon_day + " 完成</span>" +
                                        "</span>" +
                                        '<div class="whiteLine"></div> ' +
                                        "</li>";
                                }
                            }
                            else {
                                if (listType == 0) {
                                    html += "<li class='charge-li show-hide-task' update_timestamp='" + task.update_timestamp + "' project_task_id='" + task.project_task_id + "' >" +
                                        "<p class='taskName'>" + task_name + "</p>" +
                                        "<span class='secondSpan'>" +
                                        "<img class='timeImg' src='../task/images/task/clockWhite.png' />" +
                                        "<span class='completeTime'>" + mon_day + " 完成</span>" +
                                        "</span>" +
                                        '<div class="liLine"></div> ' +
                                        "</li>";
                                }
                                else {
                                    html += "<li class='charge-li show-hide-task' update_timestamp='" + task.update_timestamp + "' project_task_id='" + task.project_task_id + "' >" +
                                        img_url +
                                        "<p class='taskName'>" + task_name + "</p>" +
                                        "<span class='secondSpan'>" +
                                        "<img class='timeImg' src='../task/images/task/clockWhite.png' />" +
                                        "<span class='completeTime'>" + mon_day + " 完成</span>" +
                                        "</span>" +
                                        '<div class="liLine"></div> ' +
                                        "</li>";
                                }
                            }
                        }
                        $(".finish-ul").empty().append(html);

                        showAvatar(".user_pic");
                    }
                    else {
                        $(".task-alert").text("Error").show().fadeOut(2000);
                    }
                }
            },
            error: function (d) {
                $(".task-alert").text("网络不给力").show().fadeOut(2000);
            }
        })
    }

    //加载更多
    $(window).scroll(function () {
        //当内容滚动到底部时加载新的内容 100当距离最底部100个像素时开始加载.
        if ($(this).scrollTop() + $(document).height() + 100 >= $(document).height()) {

            if (stop == true) {
                stop = false;
                if (listType == 4) {
                    //讨论
                    projectDynamicList(lastComment, project_id);
                }
                else if (listType == 5) {
                    //文件
                    fileLists(project_id, lastFileId);
                }
                else if (isFynamic == true) {
                    //动态
                    dynamicLists(user_id, lastTimeStamp);
                }
                else {
                    stop = true;
                }
            }
        }
    });

    //项目任务列表
    function projectTaskList(project_id, project_task_id, status) {
        //status 任务完成情况-1全部 0 未完成 1完成
        var proTaskId = project_task_id;
        var url = apiurl + "/projects/task/list";
        var urldata = {
            user_id: user_id,
            token: token,
            project_id: project_id,
            project_task_id: project_task_id,
            status: status
        }
        $.ajax({
            url: url,
            type: "GET",
            data: urldata,
            beforeSend: function (d) {
                if (project_task_id == 0 && status == -1) {
                    $(".detailLoading").show();
                    $(".content-div").hide();
                    $(".noTask-div").hide();
                }
            },
            complete: function (d) {
                if (project_task_id == 0 && status == -1) {
                    $(".detailLoading").hide();
                }
            },
            success: function (d) {
                if (d.msg == 202) {
                    $(".token_iframe").show();
                }
                else {
                    var comData = d;
                    if (d.code == undefined) {
                        comData = JSON.parse(d);
                    }
                    if (comData.code == 0) {
                        var data = JSON.parse(d).data.list;
                        projectTaskMembers = JSON.parse(d).data.userCallbackString;
                        if (data == "") {
                            if (status == -1) {
                                //无任务
                                $(".content-div").hide();
                                $(".noTask-div").show();
                                $(".noTaskButton-div").show();
                                $("#noTask-img").attr("src", "../../images/task/task.png");
                                $(".noTask-tip").text("该项目还未创建任务");
                                $(".noTaskButton-div").text("创建任务");
                            }
                            else if (status == 1) {
                                if (proTaskId == 0) {
                                    $(".project-alert").text("当前无完成任务").show().fadeOut(2000);
                                }
                            }
                        }
                        else {
                            $(".content-div").show();
                            $(".noTask-div").hide();
                            var responseUserAvatar = '';
                            var qyxBindId = '';
                            var name = '';
                            if (status == -1) {
                                var a = 0;
                                var b = 0;
                                for (var j = 0; j < data.length; j++) {
                                    if (data[j].status == 0) {
                                        a++;
                                    }
                                }
                                for (var i = 0; i < data.length; i++) {
                                    for (key in projectTaskMembers) {
                                        if (data[i].response_id == projectTaskMembers[key].id) {
                                            responseUserAvatar = projectTaskMembers[key].avatar;
                                            qyxBindId = projectTaskMembers[key].qyxBindId;
                                            name = projectTaskMembers[key].name;
                                        }
                                    }

                                    var img_url;
                                    //if (responseUserAvatar == "") {
                                        var color = avatar_getColor(qyxBindId);
                                        img_url = "<div class='user_pic_div_projectTask' style='background-color: " + color + "'>" + avatar_name(name) + "</div>"
                                    //} else {
                                    if (responseUserAvatar.length > 0) {
                                        var src = avatar_getUrl(localStorage.user_id, localStorage.token, responseUserAvatar);
                                        img_url += "<img src='" + src + "' class='user_pic_projectTask' hidden>";
                                    }

                                    var title = data[i].project_task_name;
                                    var date = new Date(data[i].deadline);
                                    var project_task_id = data[i].project_task_id;
                                    initDate(date);
                                    var taskDate = date;
                                    taskDate.setHours(0);
                                    taskDate.setMinutes(0);
                                    taskDate.setSeconds(0);
                                    taskDate.setMilliseconds(0);
                                    var today = new Date();
                                    today.setHours(24);
                                    today.setMinutes(0);
                                    today.setSeconds(0);
                                    today.setMilliseconds(0);
                                    if (taskDate < today) {
                                        //延期
                                        if (data[i].status == 0) {
                                            b++;
                                            if (b == a) {
                                                $(".charge-ul").append("<li class='projectTaskCharge-li' project_task_id='" + project_task_id + "'>" +
                                                    img_url +
                                                    "<span class='firstSpan'>" +
                                                    "<p class='taskName'>" + title + "</p></span>" +
                                                    "<span class='secondSpan'>" +
                                                    "<img class='timeImg' src='../../images/task/clockRed.png' />" +
                                                    "<span class='deadline'>" + mon_day + "</span></span>" +
                                                    '<div class="whiteLine"></div> ' +
                                                    "</li>")
                                            }
                                            else {
                                                $(".charge-ul").append("<li class='projectTaskCharge-li' project_task_id='" + project_task_id + "'>" +
                                                    img_url +
                                                    "<span class='firstSpan'>" +
                                                    "<p class='taskName'>" + title + "</p></span>" +
                                                    "<span class='secondSpan'>" +
                                                    "<img class='timeImg' src='../../images/task/clockRed.png' />" +
                                                    "<span class='deadline'>" + mon_day + "</span></span>" +
                                                    '<div class="liLine"></div> ' +
                                                    "</li>")
                                            }
                                        }
                                    }
                                    else {
                                        if (data[i].status == 0) {
                                            b++;
                                            if (b == a) {
                                                $(".charge-ul").append("<li class='projectTaskCharge-li' project_task_id='" + project_task_id + "'>" +
                                                    img_url +
                                                    "<span class='firstSpan'>" +
                                                    "<p class='taskName'>" + title + "</p></span>" +
                                                    "<span class='secondSpan'>" +
                                                    "<img class='timeImg' src='../../images/task/clockWhite.png' />" +
                                                    "<span class='afterdeadline'>" + mon_day + "</span></span>" +
                                                    '<div class="whiteLine"></div> ' +
                                                    "</li>")
                                            }
                                            else {
                                                $(".charge-ul").append("<li class='projectTaskCharge-li' project_task_id='" + project_task_id + "'>" +
                                                    img_url +
                                                    "<span class='firstSpan'>" +
                                                    "<p class='taskName'>" + title + "</p></span>" +
                                                    "<span class='secondSpan'>" +
                                                    "<img class='timeImg' src='../../images/task/clockWhite.png' />" +
                                                    "<span class='afterdeadline'>" + mon_day + "</span></span>" +
                                                    '<div class="liLine"></div> ' +
                                                    "</li>")
                                            }
                                        }
                                    }

                                }
                                showAvatar(".user_pic_projectTask");
                            }
                            else if (status == 1) {
                                for (var i = 0; i < data.length; i++) {
                                    for (key in projectTaskMembers) {
                                        if (data[i].response_id == projectTaskMembers[key].id) {
                                            responseUserAvatar = projectTaskMembers[key].avatar;
                                            qyxBindId = projectTaskMembers[key].qyxBindId;
                                            name = projectTaskMembers[key].name;
                                        }
                                    }
                                    var img_url;
                                    //if (responseUserAvatar == "") {
                                        var color = avatar_getColor(qyxBindId);
                                        img_url = "<div class='user_pic_div_projectTaskFinish' style='background-color: " + color + "'>" + avatar_name(name) + "</div>"
                                    //} else {
                                    if (responseUserAvatar.length > 0) {
                                        var src = avatar_getUrl(localStorage.user_id, localStorage.token, responseUserAvatar);
                                        img_url += "<img src='" + src + "' class='user_pic_projectTaskFinish' hidden>";
                                    }

                                    var taskTitle = data[i].project_task_name;
                                    var date = new Date(data[i].complete_timestamp);
                                    var project_task_id = data[i].project_task_id;
                                    initDate(date);
                                    if (i == data.length - 1) {
                                        $(".finish-ul").append("<li class='projectTaskCharge-li show-hide-task' user_id='" + user_id + "' project_task_id='" + project_task_id + "'>" +
                                            img_url +
                                            "<p class='taskName'>" + taskTitle + "</p>" +
                                            "<span class='secondSpan'><img class='timeImg' src='../../images/task/clockWhite.png' /><span class='completeTime'>" + mon_day + " 完成</span></span>" +
                                            '<div class="whiteLine"></div> ' +
                                            "</li>")
                                    }
                                    else {
                                        $(".finish-ul").append("<li class='projectTaskCharge-li show-hide-task' user_id='" + user_id + "' project_task_id='" + project_task_id + "'>" +
                                            img_url +
                                            "<p class='taskName'>" + taskTitle + "</p>" +
                                            "<span class='secondSpan'><img class='timeImg' src='../../images/task/clockWhite.png' /><span class='completeTime'>" + mon_day + " 完成</span></span>" +
                                            '<div class="liLine"></div> ' +
                                            "</li>")
                                    }
                                }
                                showAvatar(".user_pic_projectTaskFinish");
                            }

                        }
                    }
                    else {
                        $(".project-alert").text("Error").show().fadeOut(2000);
                    }
                }
            },
            error: function (d) {
                $(".project-alert").text("网络不给力").show().fadeOut(2000);
            }
        })
    }

    // 前往任务详情
    $(document).on('click', '.charge-li', function () {
        var project_task_id = $(this).attr("project_task_id");
        window.location.href = "../task/taskDetail.html?id=" + project_task_id + "&detailBack=0&taskListBack=" + listType;
    })

    $(document).on('click', '.projectTaskCharge-li', function () {
        var project_task_id = $(this).attr("project_task_id");
        window.location.href = "../task/taskDetail.html?id=" + project_task_id + "&detailBack=5";
    })

    //项目文件列表
    function fileLists(project_id, file_id) {
        var url = apiurl + "/projects/file/list";
        var urlData = {
            project_id: project_id,
            project_file_id: file_id
        }
        $.ajax({
            url: url,
            type: "GET",
            data: urlData,
            beforeSend: function (d) {
                if (file_id == 0) {
                    $(".detailLoading").show();
                    $(".content-div").hide();
                    $(".noTask-div").hide();
                }
            },
            complete: function (d) {
                if (file_id == 0) {
                    $(".detailLoading").hide();
                }
            },
            success: function (d) {
                var comData = d;
                if (d.code == undefined) {
                    comData = JSON.parse(d);
                }
                if (comData.code == 0) {
                    var data = JSON.parse(d).data;
                    if (data == "") {
                        //无任务
                        if (file_id == 0) {
                            $(".content-div").hide();
                            $(".noTask-div").show();
                            $(".noTaskButton-div").show();
                            $("#noTask-img").attr("src", "../../images/project/file.png");
                            $(".noTask-tip").text("还未上传任何文件");
                            $(".noTaskButton-div").text("上传文件");
                        }
                    }
                    else {
                        if (data.length == 10) {
                            stop = true;
                        }
                        $(".content-div").show();
                        $(".noTask-div").hide();
                        for (var i = 0; i < data.length; i++) {
                            var file_url = data[i].project_url;
                            var file_name = data[i].project_file_name;
                            lastFileId = data[i].project_file_id;


                            var arr = file_name.split(".");
                            var type = '';
                            var imgUrl = '';
                            if (arr[arr.length - 1] == "jpg" || arr[arr.length - 1] == "png" || arr[arr.length - 1] == "jpeg") {
                                type = "project-pic-img";
                                imgUrl = '../../images/task/pic.png';
                            }
                            else {
                                type = "project-img";
                                imgUrl = '../../images/project/word.png';
                            }

                            $("#projectFile-ul").append("<li class='project-li' file_data_down = '" + file_url + "' file_name_down='" + file_name + "'>" +
                                "<img class='" + type + "' src='" + imgUrl + "' />" +
                                "<span class='project-span'>" + file_name + "</span>" +
                                "<hr class='bottomLine' /></li>")
                        }

                        $(document).on('click', '.project-li', function () {
                            $(".call_back_down_p").show();
                            file_data_down = $(this).attr("file_data_down");
                            file_name_down = $(this).attr("file_name_down");
                        })
                    }
                }
                else {
                    stop = true;
                }

            },
            error: function (d) {
                stop = true;
            }
        })
    }

    //获取项目讨论列表
    function projectDynamicList(project_task_comment_id, project_id) {
        var url = apiurl + "/projects/task/talkList";
        var urlData = {
            user_id: user_id,
            token: token,
            project_id: project_id,
            project_task_comment_id: project_task_comment_id
        }
        $.ajax({
            url: url,
            type: "GET",
            data: urlData,
            beforeSend: function (d) {
                if (project_task_comment_id == 0) {
                    $(".detailLoading").show();
                    $(".content-div").hide();
                    $(".noTask-div").hide();
                }
            },
            complete: function (d) {
                if (project_task_comment_id == 0) {
                    $(".detailLoading").hide();
                }
            },
            success: function (d) {
                if (d.msg == 202) {
                    $(".token_iframe").show();
                    stop = false;
                }
                else {
                    var comData = d;
                    if (d.code == undefined) {
                        comData = JSON.parse(d);
                    }
                    if (comData.code == 0) {
                        var data = JSON.parse(d).data.list;
                        projectCommentMembers = JSON.parse(d).data.userCallbackString;
                        if (data == "") {
                            if (project_task_comment_id == 0) {
                                //无任务
                                $(".content-div").hide();
                                $(".noTask-div").show();
                                $(".noTaskButton-div").hide();
                                $("#noTask-img").attr("src", "../../images/project/discussion.png");
                                $(".noTask-tip").text("该项目还未发起讨论");
                            }
                        }
                        else {
                            if (data.length == 10) {
                                stop = true;
                            }
                            $(".content-div").show();
                            $(".noTask-div").hide();
                            for (var i = 0; i < data.length; i++) {
                                var comment = data[i];
                                lastComment = comment.project_task_comment_id;
                                initDate(new Date(data[i].timestamp));

                                var commentUserName = '';
                                var commentUserAvatar = '';
                                var qyxBindId = '';
                                for (key in projectCommentMembers) {
                                    if (comment.user_id == projectCommentMembers[key].id) {
                                        commentUserName = projectCommentMembers[key].name;
                                        commentUserAvatar = projectCommentMembers[key].avatar;
                                        qyxBindId = projectCommentMembers[key].qyxBindId;
                                    }
                                }
                                var img_url;
                                //if (commentUserAvatar == "") {
                                    var color = avatar_getColor(qyxBindId);
                                    img_url = "<div class='user_pic_div_pc' style='background-color: " + color + "'>" + avatar_name(commentUserName) + "</div>"
                                //} else {
                                if (commentUserAvatar.length > 0) {
                                    var src = avatar_getUrl(localStorage.user_id, localStorage.token, commentUserAvatar);
                                    img_url += "<img src='" + src + "' class='user_pic_pc' hidden>";
                                }
                                var html = "";
                                var htmlLi = "";
                                if (comment.comment_file.length > 0) {
                                    html = '<li class="projectComment-li" taskId="' + comment.project_task_id + '" del="' + comment.del_flag + '">' +
                                        '<div class="span-left">' +
                                        img_url +
                                        '<p class="name">' + commentUserName + '</p></div>' +
                                        '<div class="span-right">' +
                                        '<p class="taskName-p">' + data[i].project_task_name + '</p>' +
                                        '<p class="discussion">' + data[i].comment_body + '</p>' +
                                        '<div class="commentFileDiv">' +
                                        '<ul class="commentFileUl" id="' + comment.project_task_comment_id + '">' +
                                        '</ul>' +
                                        '</div>' +
                                        '<p class="date">' + mon_day + '</p>' +
                                        '</div>' +
                                        '<div class="clear"></div>' +
                                        '<hr class="bottomLine" />' +
                                        '</li>';

                                    for (file in comment.files) {

                                        var arr = comment.files[file].file_name.split(".");
                                        var type = '';
                                        var imgUrl = '';
                                        if (arr[arr.length - 1] == "jpg" || arr[arr.length - 1] == "png" || arr[arr.length - 1] == "jpeg") {
                                            type = "commentPicImg";
                                            imgUrl = '../../images/task/pic.png';
                                        }
                                        else {
                                            type = "commentFileImg";
                                            imgUrl = '../../images/project/word.png';
                                        }

                                        htmlLi += '<li class="commentFileLi">' +
                                            '<img class="' + type + '" src="' + imgUrl + '"/>' +
                                            '<p class="commentFileName">' + comment.files[file].file_name + '</p>' +
                                            '</li>'
                                    }
                                }
                                else {
                                    html = '<li class="projectComment-li" taskId="' + comment.project_task_id + '" del="' + comment.del_flag + '">' +
                                        '<div class="span-left">' +
                                        img_url +
                                        '<p class="name">' + commentUserName + '</p></div>' +
                                        '<div class="span-right">' +
                                        '<p class="taskName-p">' + data[i].project_task_name + '</p>' +
                                        '<p class="discussion">' + data[i].comment_body + '</p>' +
                                        '<p class="date">' + mon_day + '</p></div>' +
                                        '<div class="clear"></div>' +
                                        '<hr class="bottomLine" />' +
                                        '</li>';
                                }

                                $(".project-ul").append(html);
                                var ulId = "#" + comment.project_task_comment_id;
                                $(ulId).append(htmlLi);
                            }
                        }
                        showAvatar(".user_pic_pc");
                    }
                    else {
                        stop = true;
                    }
                }
            },
            error: function (d) {
                stop = true;
            }
        })
    }

    // 前往任务详情
    $(document).on('click', '.projectComment-li', function () {
        var project_task_id = $(this).attr("taskId");
        var del_flag = $(this).attr("del");
        if (del_flag == 0) {
            window.location.href = "../task/taskDetail.html?id=" + project_task_id + "&detailBack=5&projectComment=1";
        }
        else {
            $(".project-alert").text("该任务已被删除").show().fadeOut(2000);
        }
    });

    function showAvatar(classname){
        $(classname).one("load",function(){
            $(this).show();
            $(this).prev().hide();
        });
    }

})(jQuery);
